﻿/**
 * @name	jQuery.touchSlider
 * @author	dohoons ( http://dohoons.com/ )
 *
 * @version	201208_6
 * @since	201106
 *
 * @param Object	settings	환경변수 오브젝트
 *		roll			-	순환 (default true)
 *		flexible		-	유동 레이아웃 (default false)
 *		view			-	다중 컬럼 (default 1)
 *		speed			-	애니메이션 속도 (default 75)
 *		range			-	넘김 판정 범위 (default 0.15)
 *		page			-	초기 페이지 (default 1)
 *		transition		-	CSS3 transition 사용 (default false)
 *		btn_prev		-	prev 버튼 (jQuery Object, default null)
 *		btn_next		-	next 버튼 (jQuery Object, default null)
 *		paging			-	page 버튼 (jQuery Object, default null)
 *		initComplete	-	초기화 콜백
 *		counter			-	슬라이드 콜백, 카운터
 *
 * @example

	$("#target").touchSlider({
		flexible : true
	});

*/

(function ($) {

	$.fn.touchSlider = function (settings) {

		settings.supportsCssTransitions = (function (style) {
			var prefixes = ['Webkit','Moz','Ms','O'];
			for(var i=0, l=prefixes.length; i < l; i++ ) {
				if( typeof style[prefixes[i] + 'Transition'] !== 'undefined') {
					return true;
				}
			}
			return false;
		})(document.createElement('div').style);

		settings = jQuery.extend({
			roll : true,
			flexible : false,
			btn_prev : null,
			btn_next : null,
			paging : null,
			speed : 75,
			view : 1,
			range : 0.15,
			page : 1,
			transition : false,
			initComplete : null,
			counter : null
		}, settings);

		var opts = [];
		opts = $.extend({}, $.fn.touchSlider.defaults, settings);

		return this.each(function () {

			$.fn.extend(this, touchSlider);

			var _this = this;

			this.opts = opts;
			this._view = this.opts.view;
			this._speed = this.opts.speed;
			this._tg = $(this);
			this._list = this._tg.children("ul");
			this._width = parseInt(this._tg.css("width"));
			//this._item_L = parseInt(this._list.css("left").replace("px",""));
			this._item_w2 = this._list.width();
			this._len = this._list.length;
			this._range = this.opts.range * this._width;
			this._pos = [];
			this._start = [];
			this._startX = 0;
			this._startY = 0;
			this._left = 0;
			this._top = 0;
			this._drag = false;
			this._scroll = false;
			this._btn_prev;
			this._btn_next;

			this.init();

			$(this)
			.bind("touchstart", this.touchstart)
			.bind("touchmove", this.touchmove)
			.bind("touchend", this.touchend)
			.bind("dragstart", this.touchstart)
			.bind("drag", this.touchmove)
			.bind("dragend", this.touchend)

			$(window).bind("orientationchange resize", function () {
				_this.resize(_this);
			});
		});

	};

	var touchSlider = {

		init : function () {
			var _this = this;

			$(this).children().css({
				//"width":this._width + "px",
				"overflow":"visible"
			});


			this.counter();
			this.initComplete();

			var liWidth = [];
			var liWidth2 = [];
			$("#touchSlider > ul").each(function(i){
				 liWidth[i] = $(this).width();
				 if(i==0){
					liWidth2[i] = liWidth[i];
				}else{
					liWidth2[i] = liWidth2[i-1] + liWidth[i];
				}
			});
			liWidth2.unshift(liWidth[0]*0);
			for(var i=0; i<this._len; ++i) {
				this._pos[i] =  liWidth2[i];
				this._start[i] = this._pos[i];
			}
		},

		initComplete : function () {
			if(typeof(this.opts.initComplete) == "function") {
				this.opts.initComplete(this);
			}
		},

		resize : function (e) {
			if(e.opts.flexible) {
				var tmp_w = e._item_w;

				e._width = parseInt(e._tg.css("width"));
				e._item_w = e._width / e._view;
				e._range = e.opts.range * e._width;

				var $ulEleLeft = $("#touchSlider > ul").css("left").replace("px","");
				if($ulEleLeft == 0){
					var liWidth = [];
					var liWidth2 = [];
					$("#touchSlider > ul > li").each(function(i){
						 liWidth[i] = $(this).width();
						 if(i==0){
							liWidth2[i] = liWidth[i];
						}else{
							liWidth2[i] = liWidth2[i-1] + liWidth[i];
						}
					});
					liWidth2.unshift(liWidth[0]*0);
					for(var i=0; i<this._len; ++i) {
						this._pos[i] =  liWidth2[i];
						this._start[i] = this._pos[i];
					}
				}

				var $ulEle = $("#touchSlider > ul");
				var $ulEleLeft = $ulEle.css("left").replace("px","");
				var $ulEleLeft2 = $ulEleLeft.replace("-","");
				var liWidth = [];
				var liWidth2 = [];
				var wHalfArray = [];
				var wHalfArray2 = [];
				var Wwh = $("body").width();
				var Hwh = $("#header").width();
				var viewArea = Wwh - Hwh;

				$("#touchSlider > ul > li").each(function(i){
					 liWidth[i] = $(this).width();
					 wHalfArray[i] = liWidth[i] /2;
					 if(i==0){
						liWidth2[i] = liWidth[i];
					}else{
						liWidth2[i] = liWidth2[i-1] + liWidth[i];
						wHalfArray2[i] = -liWidth2[i-1] + -wHalfArray[i];
					}
				});

				var fisrLiLeftState = liWidth2[5] - viewArea;
				function rightAlign(){
					var fisrLiLeftState = liWidth2[5] - viewArea;
					if($ulEle.css("left").replace("-","").replace("px","") > fisrLiLeftState){
						$ulEle.css("left",-fisrLiLeftState + "px");
						if ((navigator.userAgent.indexOf ("Firefox") != -1) || (navigator.userAgent.indexOf ("Opera") != -1)){
							$('.ImagesArea').css({'left':-fisrLiLeftState + 160 + 'px'});
							$('.mapIn').css({'left':-fisrLiLeftState + 160 + 'px'});
							$('.ImagesArea div').each( function () {
								var aa = $(this).css('backgroundPosition').split(' ');
								$(this).css({'backgroundPosition':-fisrLiLeftState + 160 + 'px ' + aa[1]});
							});
						} else {
							$('.ImagesArea').css({'left':-fisrLiLeftState + 160 + 'px'});
							$('.mapIn').css({'left':-fisrLiLeftState + 160 + 'px'});
							$('.ImagesArea div').each( function () {
								var aa = $(this).css('background-position-y');
								$(this).css({'background-position':-fisrLiLeftState + 160 + "px " + aa});
							});
						}
					}
				}

				if($ulEle.css("left").replace("-","").replace("px","") >= fisrLiLeftState+1){
                    rightAlign();
                } else if($ulEleLeft > 0 || $ulEleLeft > -wHalfArray[0]){
					$ulEle.css("left","0px");
					if ((navigator.userAgent.indexOf ("Firefox") != -1) || (navigator.userAgent.indexOf ("Opera") != -1)){
						$('.ImagesArea').css({'left':"160px "});
						$('.mapIn').css({'left':"160px "});
						$('.ImagesArea div').each( function () {
							var aa = $(this).css('backgroundPosition').split(' ');
							$(this).css({'backgroundPosition':"160px " + aa[1]});
						});
					} else {
						$('.ImagesArea').css({'left':"160px"});
						$('.mapIn').css({'left':"160px"});
						$('.ImagesArea div').each( function () {
							var aa = $(this).css('background-position-y');
							$(this).css({'background-position':"160px " + aa});
						});
					}
				} else if ($ulEleLeft < -wHalfArray[0] && $ulEleLeft > wHalfArray2[1]){
					$ulEle.css("left",-liWidth2[0]+1);
					if ((navigator.userAgent.indexOf ("Firefox") != -1) || (navigator.userAgent.indexOf ("Opera") != -1)){
						$('.ImagesArea').css({'left':"-300px"});
						$('.mapIn').css({'left':"-300px"});
						$('.ImagesArea div').each( function () {
							var aa = $(this).css('backgroundPosition').split(' ');
							$(this).css({'backgroundPosition':"-160px " + aa[1]});
						});
					} else {
						$('.ImagesArea').css({'left':"-300px"});
						$('.mapIn').css({'left':"-300px"});
						$('.ImagesArea div').each( function () {
							var aa = $(this).css('background-position-y');
							$(this).css({'background-position':"-300px " + aa});
						});
					}
					rightAlign();
				} else if ($ulEleLeft < wHalfArray2[1] && $ulEleLeft > wHalfArray2[2]){
					$ulEle.css("left",-liWidth2[1]+1);
					rightAlign();
				} else if ($ulEleLeft < wHalfArray2[2] && $ulEleLeft > wHalfArray2[3]){
					$ulEle.css("left",-liWidth2[2]+1);
					rightAlign();
				} else if ($ulEleLeft < wHalfArray2[3] && $ulEleLeft > wHalfArray2[4]){
					$ulEle.css("left",-liWidth2[3]+1);
					rightAlign();
				} else if ($ulEleLeft < wHalfArray2[4] && $ulEleLeft > wHalfArray2[5]){
					$ulEle.css("left",-liWidth2[4]+1);
					rightAlign();
				} else {
					$ulEle.css("left",-liWidth2[5]+1);
					rightAlign();
				}
			}

			this.counter();
		},

		touchstart : function (e) {
			if((e.type == "touchstart" && e.originalEvent.touches.length <= 1) || e.type == "dragstart") {
				this._startX = e.pageX || e.originalEvent.touches[0].pageX;
				//this._startY = e.pageY || e.originalEvent.touches[0].pageY;
				this._scroll = false;
				this._start = [];

				var $ulEle = $("#touchSlider > ul");
				var $ulEleLeft = $ulEle.css("left").replace("px","");
				var liWidth = [];
				var liWidth2 = [];
				var Wwh = $("body").width();
				var Hwh = $("#header").width();
				var viewArea = Wwh - Hwh;
				$("#touchSlider > ul > li").each(function(i){
					 liWidth[i] = $(this).width();
					 if(i==0){
						liWidth2[i] = liWidth[i];
					}else{
						liWidth2[i] = liWidth2[i-1] + liWidth[i];
					}
				});
				liWidth2.unshift(liWidth[0]*0);

				var fisrLiLeftState = liWidth2[5] - viewArea;

				var i = 0;
				if ($ulEleLeft == 0){
					this._pos[i] =  liWidth2[0];
				}
				if ($ulEle.css("left").replace("-","").replace("px","") == 0){
					var ulLeftInit = -1;
				} else {
					var ulLeftInit = -$ulEle.css("left").replace("-","").replace("px","")
				}
				this._start[i] = ulLeftInit;
			}
		},

		touchmove : function (e) {
			if((e.type == "touchmove" && e.originalEvent.touches.length <= 1) || e.type == "drag") {
				this._left = (e.pageX || e.originalEvent.touches[0].pageX) - this._startX;
				//this._top = (e.pageY || e.originalEvent.touches[0].pageY) - this._startY;
				var w = this._left < 0 ? this._left * -1 : this._left;
				var h = this._top < 0 ? this._top * -1 : this._top;

				if (w < h || this._scroll) {
					this._left = 0;
					this._drag = false;
					this._scroll = true;
				} else {
					e.preventDefault();
					this._drag = true;
					this._scroll = false;
					this.position(e);
				}

				var $ulEleLeft = $("#touchSlider > ul").css("left").replace("px","");
				var Wwh = $("body").width();
				var Hwh = $("#header").width();
				var viewArea = Wwh - Hwh;
				var liWidth = [];
				var liWidth2 = [];

				$("#touchSlider > ul > li").each(function(i){
					 liWidth[i] = $(this).width();
					 if(i==0){
						liWidth2[i] = liWidth[i];
					}else{
						liWidth2[i] = liWidth2[i-1] + liWidth[i];
					}
				});
				var fisrLiLeftState = liWidth2[5] - viewArea;

				for(var i=0; i<this._len; ++i) {
					if(inko == 11){
						return false;
					} else {
						var tmp = this._start[i] + this._left;
						if(this.opts.supportsCssTransitions && this.opts.transition) {
							var trans = "translate3d(" + tmp + "px,0,0)";
							this._list.eq(i).css({
								"left" : "",
								"-o-transition" : "0ms",
								"-o-transform" : trans,
								"-moz-transition" : "0ms",
								"-moz-transform" : trans,
								"-ms-transition" : "0ms",
								"-ms-transform" : trans,
								"-webkit-transition" : "0ms",
								"-webkit-transform" : trans,
								"transition" : "0ms",
								"transform" : trans
							});
						} else {
							if ((navigator.userAgent.indexOf ("Firefox") != -1) || (navigator.userAgent.indexOf ("Opera") != -1)){
								$('.ImagesArea').css({'left':tmp + 160 + "px "});
								$('.mapIn').css({'left':tmp + 160 + "px "});
								$('.ImagesArea div').each( function () {
									var aa = $(this).css('backgroundPosition').split(' ');
									$(this).css({'backgroundPosition':tmp + 160 + "px " + aa[1]});
								});
							} else {
								$('.ImagesArea').css({'left':tmp + 160 + "px "});
								$('.mapIn').css({'left':tmp + 160 + "px "});
								$('.ImagesArea div').each( function () {
									var aa = $(this).css('background-position-y');
									$(this).css({'background-position':tmp + 160 + "px " + aa});
								});
							}
							this._list.eq(i).css("left", tmp + "px");
						}
						this._pos[i] = tmp;
					}
				}
			}
		},

		touchend : function (e) {
			if((e.type == "touchend" && e.originalEvent.touches.length <= 1) || e.type == "dragend") {
				var $ulEle = $("#touchSlider > ul");
				var $ulEleLeft = $ulEle.css("left").replace("px","");
				var $ulEleLeft2 = $ulEleLeft.replace("-","");
				var liWidth = []; // li들의 너비
				var liWidth2 = []; // li들의 너비를 더한거
				var wHalfArray = [];
				var wHalfArray2 = [];
				var Wwh = $("body").width();
				var Hwh = $("#header").width();
				var viewArea = Wwh - Hwh;

				$("#touchSlider > ul > li").each(function(i){
					 liWidth[i] = $(this).width();
					 wHalfArray[i] = liWidth[i] /2;
					 if(i==0){
						liWidth2[i] = liWidth[i];
					}else{
						liWidth2[i] = liWidth2[i-1] + liWidth[i];
						wHalfArray2[i] = -liWidth2[i-1] + -wHalfArray[i];
					}
				});

				var fisrLiLeftState = liWidth2[5] - viewArea;
				function rightAlign(){
					if($ulEle.css("left").replace("-","").replace("px","") > fisrLiLeftState){
						$ulEle.css("left",-fisrLiLeftState + "px");
						if ((navigator.userAgent.indexOf ("Firefox") != -1) || (navigator.userAgent.indexOf ("Opera") != -1)){
							$('.ImagesArea').css({'left':-fisrLiLeftState + 160 + 'px'});
							$('.mapIn').css({'left':-fisrLiLeftState + 160 + 'px'});
							$('.ImagesArea div').each( function () {
								var aa = $(this).css('backgroundPosition').split(' ');
								$(this).css({'backgroundPosition':-fisrLiLeftState + 160 + 'px ' + aa[1]});
							});
						} else {
							$('.ImagesArea').css({'left':-fisrLiLeftState + 160 + 'px'});
							$('.mapIn').css({'left':-fisrLiLeftState + 160 + 'px'});
							$('.ImagesArea div').each( function () {
								var aa = $(this).css('background-position-y');
								$(this).css({'background-position':-fisrLiLeftState + 160 + "px " + aa});
							});
						}
					}
				}

				if (this._left > 0){
					$("#touchSlider > ul").css("left","0px");
				}
				
				if($ulEle.css("left").replace("-","").replace("px","") >= fisrLiLeftState+1){
					rightAlign();
				} else if($ulEleLeft > 0 || $ulEleLeft > -wHalfArray[0]){
					$ulEle.css("left","0px");
					if ((navigator.userAgent.indexOf ("Firefox") != -1) || (navigator.userAgent.indexOf ("Opera") != -1)){
						$('.ImagesArea').css({'left':"160px "});
						$('.mapIn').css({'left':"160px "});
						$('.ImagesArea div').each( function () {
							var aa = $(this).css('backgroundPosition').split(' ');
							$(this).css({'backgroundPosition':"160px " + aa[1]});
						});
					} else {
						$('.ImagesArea').css({'left':"160px"});
						$('.mapIn').css({'left':"160px"});
						$('.ImagesArea div').each( function () {
							var aa = $(this).css('background-position-y');
							$(this).css({'background-position':"160px " + aa});
						});
					}
					rightAlign();
				} else if ($ulEleLeft < -wHalfArray[0] && $ulEleLeft > wHalfArray2[1]){
					$ulEle.css("left",-liWidth2[0]+1);
					if ((navigator.userAgent.indexOf ("Firefox") != -1) || (navigator.userAgent.indexOf ("Opera") != -1)){						
						$('.ImagesArea').css({'left':"-300px"});
						$('.mapIn').css({'left':"-300px"});
						$('.ImagesArea div').each( function () {
							var aa = $(this).css('backgroundPosition').split(' ');
							$(this).css({'backgroundPosition':"-160px " + aa[1]});
						});
					} else {
						$('.ImagesArea').css({'left':"-300px"});
						$('.mapIn').css({'left':"-300px"});
						$('.ImagesArea div').each( function () {
							var aa = $(this).css('background-position-y');
							$(this).css({'background-position':"-300px " + aa});
						});
					}
					rightAlign();
				} else if ($ulEleLeft < wHalfArray2[1] && $ulEleLeft > wHalfArray2[2]){
					$ulEle.css("left",-liWidth2[1]+1);
					rightAlign();
				} else if ($ulEleLeft < wHalfArray2[2] && $ulEleLeft > wHalfArray2[3]){
					$ulEle.css("left",-liWidth2[2]+1);
					rightAlign();
				} else if ($ulEleLeft < wHalfArray2[3] && $ulEleLeft > wHalfArray2[4]){
					rightAlign();
					$ulEle.css("left",-liWidth2[3]+1);
					rightAlign();
				} else if ($ulEleLeft < wHalfArray2[4] && $ulEleLeft > wHalfArray2[5]){
					$ulEle.css("left",-liWidth2[4]+1);
					rightAlign();
				} else {
					$ulEle.css("left",-liWidth2[5]+1);
					rightAlign();
				}
			}
		},

		position : function (d) {
			var gap = this._view * this._item_w;

			if(d == -1 || d == 1) {
				this._startX = 0;
				this._start = [];
				for(var i=0; i<this._len; ++i) {
					this._start[i] = this._pos[i];
				}
				this._left = d * gap;
			} else {
				//if(this._left > gap) this._left = gap;
				//if(this._left < - gap) this._left = - gap;
			}

			if(this.opts.roll) {
				var tmp_pos = [];
				for(var i=0; i<this._len; ++i) {
					tmp_pos[i] = this._pos[i];
				}
				tmp_pos.sort(function(a,b){return a-b;});

				var max_chk = tmp_pos[this._len-this._view];
				var p_min = $.inArray(tmp_pos[0], this._pos);
				var p_max = $.inArray(max_chk, this._pos);


				if(this._view <= 1) max_chk = this._len - 1;

				if((d == 1 && tmp_pos[0] >= 0) || (this._drag && tmp_pos[0] > 0)) {
					/*
					for(var i=0; i<this._view; ++i, ++p_min, ++p_max) {
						this._start[p_max] = this._start[p_min] - gap;
						this._list.eq(p_max).css("left", this._start[p_max] + "px");
					}
					*/
				} else if((d == -1 && tmp_pos[max_chk] <= 0) || (this._drag && tmp_pos[max_chk] <= 0)) {
					/*
					for(var i=0; i<this._view; ++i, ++p_min, ++p_max) {
						this._start[p_min] = this._start[p_max] + gap;
						this._list.eq(p_min).css("left", this._start[p_min] + "px");
					}
					*/
				}
			} else {
				//if(this.limit_chk()) this._left = this._left / 2;
			}
		},

		counter : function () {
			if(typeof(this.opts.counter) == "function") {
				var param = {
					total : Math.ceil(this._len / this._view),
					current : ($.inArray(0, this._pos) / this._view) + 1
				};
				this.opts.counter(param);
			}
		}

	};

})(jQuery);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    